import 'package:get/get.dart';
import 'package:ball_lion/module/match/detail/lq_match_detail_logic.dart';
import 'package:ball_lion/module/match/detail/zq_match_detail_logic.dart';
import 'package:ball_lion/entity/match/secret_info_entity.dart';
import 'package:ball_lion/entity/match/zq_match_intelligence_entity.dart';
import 'package:ball_lion/server/api/soccer_api.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/entity/match/detail/lq_detail_head_entity.dart';
import 'package:ball_lion/entity/match/zq_match_info_entity.dart';

class LqMatchInfoLogic extends IntelligenceLogic {
  final LqMatchDetailLogic detailC =
      Get.find<LqMatchDetailLogic>(tag: "${Get.arguments}");

  LqDetailHeadInfoEntity? get detail => detailC.detail;

  @override
  void onReady() {
    Utils.onEvent('bs_bl');
    super.onReady();
    getData();
  }

  Future getData() async {
    _requestData(detailC.matchId, false);
  }
}

class ZqMatchInfoLogic extends IntelligenceLogic {
  final _detailController =
      Get.find<ZqMatchDetailLogic>(tag: '${Get.arguments}');

  ZqMatchInfoEntity? get detailInfo => _detailController.info;
  BaseInfo? get detail => _detailController.info?.baseInfo;

  @override
  void onReady() async {
    Utils.onEvent('bs_bl');
    getData();
    super.onReady();
  }

  Future getData() async {
    await _requestData(_detailController.matchId, true);
  }
}

class IntelligenceLogic extends GetxController
    with GetSingleTickerProviderStateMixin {
  Map<int, List<String>>? _intelligence;
  int _currentIndex = 0;
  int _totalLength = 0;
  List<List<String>> homeSuspend = [];
  List<List<String>> guestSuspend = [];
  ZqMatchIntelligenceEntity? _data;
  List<SecretInfoEntity>? secret;

  ZqMatchIntelligenceEntity? get data => _data;
  int get totalLength => _totalLength;
  int get currentIndex => _currentIndex;
  Map<int, List<String>>? get intelligence => _intelligence;

  set intelligence(Map<int, List<String>>? value) {
    _intelligence = value;
    update();
  }

  set totalLength(int value) {
    _totalLength = value;
    update();
  }

  set currentIndex(int value) {
    _currentIndex = value;
    update();
  }

  Future _requestData(int matchId, bool isZq) async {
    _data = await SoccerApi.getIntelligence(matchId, isZq);
    secret = await SoccerApi.getSecretInfo(matchId, isZq);
    var map = <int, List<String>>{0: [], 1: [], 2: [], 3: [], 4: []};
    if (_data?.homeFavourable.noValue == false) {
      _data?.homeFavourable?.split("\n").forEach((element) {
        map[0]?.add(element);
      });
    }
    if (_data?.guestFavourable.noValue == false) {
      _data?.guestFavourable?.split("\n").forEach((element) {
        map[1]?.add(element);
      });
    }
    if (_data?.homeUnFavourable.noValue == false) {
      _data?.homeUnFavourable?.split("\n").forEach((element) {
        map[2]?.add(element);
      });
    }
    if (_data?.guestUnFavourable.noValue == false) {
      _data?.guestUnFavourable?.split("\n").forEach((element) {
        map[3]?.add(element);
      });
    }
    if (_data?.neutrality.noValue == false) {
      _data?.neutrality?.split("\n").forEach((element) {
        map[4]?.add(element);
      });
    }
    homeSuspend.addAll(formSuspend(_data?.homeInjury, isZq));
    guestSuspend.addAll(formSuspend(_data?.guestInjury, isZq));
    _intelligence = map;
    _intelligence?.values.forEach((element) => _totalLength += element.length);
    update();
  }

  List<List<String>> formSuspend(String? injury, bool isZq) {
    if (isZq) {
      return injury
              ?.split(',')
              .join()
              .split("(")
              .join()
              .split(")")
              .join()
              .split(RegExp(r"[\f\r\n\t\v]+"))
              .map((e) => e.split(RegExp(r"[ \n]+")))
              .toList() ??
          [];
    } else {
      return injury
              ?.split("(- -")
              .join()
              .split(")")
              .join(" ")
              .split(RegExp(r"[\f\r\n\t\v]+"))
              .map((e) => e.split(RegExp(r"[ \n]+")))
              .toList() ??
          [];
    }
  }
}
